package com.atguigu.gmall.list.service.impl;

import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.list.dao.GoodsDao;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/9/30 15:27 星期五
 * description:  搜索商品相关的接口类的实现类
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private GoodsDao goodsDao;

    /**
     * 将数据库中sku的信息增加至ES
     *
     * @param skuId
     */
    @Override
    public void addGoodsFromDbToEs(Long skuId) {
        //参数校验
        if(skuId == null){
            throw new RuntimeException("参数错误");
        }

        //查询商品的信息 =>skuInfo对象
        SkuInfo skuInfo = productFeign.getSkuInfo(skuId);
        if(skuInfo == null || skuInfo.getId() == null){
            throw new RuntimeException("商品不存在");
        }
        //将SkuInfo对象转换为Es中的pojo对象
        Goods goods = new Goods();
        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        //价格 ==>单独查询
        BigDecimal price = productFeign.getPrice(skuId);
        goods.setPrice(price.doubleValue());
        goods.setCreateTime(new Date());
        //品牌Id 品牌名字 品牌图片地址
        BaseTrademark baseTrademark =
                productFeign.getBaseTrademark(skuInfo.getTmId());
        goods.setTmId(baseTrademark.getId());
        goods.setTmName(baseTrademark.getTmName());
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());

        //分类一,二,三级Id和名字
        BaseCategoryView category =
                productFeign.getCategory(skuId);
        goods.setCategory1Id(category.getCategory1Id());
        goods.setCategory1Name(category.getCategory1Name());
        goods.setCategory2Id(category.getCategory2Id());
        goods.setCategory2Name(category.getCategory2Name());
        goods.setCategory3Id(category.getCategory3Id());
        goods.setCategory3Name(category.getCategory3Name());
        //平台信息
        List<BaseAttrInfo> baseAttrInfoList =
                productFeign.getBaseAttrInfoList(skuId);
        List<SearchAttr> attrs = baseAttrInfoList.stream().map(baseAttrInfo -> {
            //初始化
            SearchAttr searchAttr = new SearchAttr();
            //补数据
            searchAttr.setAttrId(baseAttrInfo.getId());
            searchAttr.setAttrName(baseAttrInfo.getAttrName());
            searchAttr.setAttrValue(baseAttrInfo.getAttrValueList().get(0).getValueName());
            //返回
            return searchAttr;
        }).collect(Collectors.toList());
        goods.setAttrs(attrs);
        //将数据写入es中去
        goodsDao.save(goods);
    }

    /**
     * 根据goodsId删除ES中的商品
     *
     * @param goodsId
     */
    @Override
    public void remove(Long goodsId) {
        goodsDao.deleteById(goodsId);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 为商品增加热度值
     *
     * @param goodsId
     */
    @Override
    public void addScore(Long goodsId) {
        //参数校验
        if(goodsId == null){
            return;
        }
        //使用redis执行++操作
        Double score = redisTemplate.opsForZSet().incrementScore("Goods_Score", goodsId, 1);
        //Long score = redisTemplate.opsForValue().increment("Goods_Score_" + goodsId, 1);
        //每200分同步一次
        if(score % 200 == 0){
            //将这个值写回es
            Optional<Goods> optional = goodsDao.findById(goodsId);
            if(optional.isPresent()){
                Goods goods = optional.get();
                //更新分数
                goods.setHotScore(score.longValue());
                goodsDao.save(goods);
            }
        }
    }
}
